const http = require('http')
const fs = require('fs')
const querystring = require('querystring')
const urllib = require('url')

let server = http.createServer((req, res) => {
  // get
  let _get = urllib.parse(req.url, true)

  // post
  let temp = ''
  req.on('data', (data) => {
    temp += data
  })
  req.on('end', () => {
    let _post = querystring.parse(temp)
    console.log(_get.pathname, _post, _get.query)
  })

  // fs
  let fileName = './www' + _get.pathname
  fs.readFile(fileName, (err, data) => {
    err ? res.write('404') : res.write(data)
    res.end()
  })
})

server.listen(8080)
